from selenium import webdriver
import os
from time import sleep
# ("chromedriver.exe","."),("test.txt",".")         
class test:
    def __init__(self):
        self.option = webdriver.ChromeOptions()
        self.option.add_argument("User-Agent=Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;")
        self.bro = webdriver.Chrome(options=self.option)
        self.bro.maximize_window()

    def get_names(self):#获取输入值
        paths = os.path.abspath(os.path.join(os.getcwd())+'\\test.txt')
        with open(paths,'r') as f:
            red = f.readlines()
        return red[0]

    def visit_index(self):#访问网页
        self.bro.get('https://www.baidu.com/')
        self.bro.current_window_handle
        sleep(1)
        self.bro.find_element_by_xpath('//*[@id="kw"]').clear()
        sleep(1)
        names = self.get_names()
        self.bro.find_element_by_xpath('//*[@id="kw"]').send_keys(names)
        sleep(1)
        inputs = self.bro.find_element_by_xpath('//*[@id="su"]')
        self.bro.execute_script('arguments[0].click();',inputs)
        sleep(1)
        self.bro.current_window_handle
        sleep(20)


    def run(self):
        self.visit_index()

if __name__ == "__main__":
    test().run()